#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN=4e5+5;
int t,n;
ll sum;
ll a[MAXN],b[MAXN];
priority_queue<ll> q;
map<ll,int>inq,inr;//inq：是否存在于优先队列中,inr是否存在于实际集合中
void solve(){
    ll x,cnt=0;
    cin>>x;
    if(!inr[x]){
        a[n+1]=x+(ll)10,inr[x]=1;
        if(n==0) b[++cnt]=x;
        if(n && x<a[1]) b[++cnt]=x;
        for(int i=1;i<=n;i++){
            b[++cnt]=a[i];
            if(x>=a[i] && x<a[i+1]) b[++cnt]=x;
        }
    }
    else{
        inr[x]=0;
        for(int i=1;i<=n;i++){
            if(a[i]==x) continue;
            b[++cnt]=a[i];
        }
    }
    n=cnt;
    for(int i=1;i<=n;i++) a[i]=b[i];
    ll suml=0,sumr=0;
    sum=0;
    for(int i=1;i<=n;i++){
        ll xx=sumr;
        suml+=a[i],sumr+=a[n-i+1];
        sum+=sumr-max(suml,xx);
    }
    cout<<sum<<'\n';
}
int main(){
    freopen("kte.in","r",stdin);
    freopen("kte.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
    cin>>t;
    if(t<=1000){
        while(t--) solve();
        return 0;
    }
    while(t--){
        if(t!=n-1){
            if(q.size()<=1) cout<<0<<'\n';
            else cout<<sum+q.top()<<'\n';
        }
        ll x;
        cin>>x;
        if(inr[x]){
            inr[x]=0;
            if(q.size()==1){
                q.pop(),inq[x]=0;
                continue;
            }
            if(-q.top()==x){
                q.pop(),inq[x]=0;
                while(q.size() && !inr[-q.top()]) inq[-q.top()]=0,q.pop();
                if(q.size()) sum+=q.top();
                continue;
            }
            sum-=x;
            continue;
        }
        inr[x]=1;
        if(!q.size()){
            q.push(-x),inq[x]=1;
            continue;
        }
        if(inq[x]){
            sum+=x;
            continue;
        }
        if(-q.top()>x) sum-=q.top();
        else sum+=x;
        q.push(-x),inq[x]=1;
    }
    if(q.size()<=1) cout<<0<<'\n';
    else cout<<sum+q.top()<<'\n';
}//long long，子文件夹